/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package justdoit.view.model;

import java.util.List;
import justdoit.controller.LanguageManager;
import javax.swing.table.AbstractTableModel;
import justdoit.model.Task;
import justdoit.controller.IconManager;
import justdoit.controller.TaskManager;
import justdoit.model.Priority;
import justdoit.view.ProgressBarInTable;
import justdoit.view.ProgressBarRenderer;

/**
 * The Tasks Model to displayed in the TaskListTable
 * @author Sadan & Can
 */
public class TasksTableModel extends AbstractTableModel {

    protected String[] columnNames = {
        LanguageManager.getString("table.column.priority"),
        LanguageManager.getString("table.column.taskDescription"),
        LanguageManager.getString("table.column.progress"),
        LanguageManager.getString("table.column.category"),
        LanguageManager.getString("table.column.dueDate")};
    private List<Task> tasks;

    public TasksTableModel(List<Task> tasks) {
        this.tasks = tasks;
    }

    public void setTasks(List<Task> tasks) {
        this.tasks = tasks;
    }

    public Task getTask(int row) {
        if (row < 0 || row >= this.tasks.size()) {
            return null;
        }
        return this.tasks.get(row);
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        Task task = tasks.get(row);
        task.setProgress((Integer) value);
        TaskManager.updateTask(task);
        fireTableCellUpdated(row, col);
    }

    public Object getValueAt(int row, int col) {
        Task task = tasks.get(row);
        if (columnNames[col].equals(LanguageManager.getString("table.column.priority"))) {
            if (task.getPriority().equals(Priority.HIGH)) {
                return new PriorityWithIcon(task.getPriority(),
                        IconManager.getIcon("priorityHigh.png"));
            } else if (task.getPriority().equals(Priority.MEDIUM)) {
                return new PriorityWithIcon(task.getPriority(),
                        IconManager.getIcon("priorityMedium.png"));
            } else if (task.getPriority().equals(Priority.LOW)) {
                return new PriorityWithIcon(task.getPriority(),
                        IconManager.getIcon("priorityLow.png"));
            } else {
                return "???";
            }
        } else if (columnNames[col].equals(LanguageManager.getString("table.column.taskDescription"))) {
            return task.getDescription();
        } else if (columnNames[col].equals(LanguageManager.getString("table.column.progress"))) {
            return task.getProgress();/*new ProgressBarInTable(task.getProgress())*/
        } else if (columnNames[col].equals(LanguageManager.getString("table.column.category"))) {
            return task.getCategory();
        } else if (columnNames[col].equals(LanguageManager.getString("table.column.dueDate"))) {
            if (task.getDeadLine() != null) {
                return task.getDeadLine().toString();
            } else {
                return "";
            }
        }
        return "???";
    }

    public int getRowCount() {
        return tasks.size();
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    /*
     * this is for editing the progress bar on the window -shadone
     */
    @Override
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col == 2) {
            return true;
        } else {
            return false;
        }
    }
}
